//
//  WTPictureView.swift
//  WTSinaWeibo
//
//  Created by  Wellim on 16/6/30.
//  Copyright © 2016年  Wellim. All rights reserved.
//

import UIKit

// cell 可重用标识符
private let pictureViewrusedId = "pictureCell"
// cell 之间的间距
private let itmeMargin: CGFloat = 5

// 计算cell的宽高
private let cellWH = (kScreenWidth - StautsMargin*2 - itmeMargin*2) / 3

class WTPictureView: UICollectionView {

    // 声明一个配图数组属性供外界赋值
    var pic_urls: [WTPictureInfo]?{
    
       
        didSet{
        // 给countLabel设置text
            
            countLabel.text = "\(pic_urls?.count ?? 0)"
            let size = getPictureViewSize(pic_urls?.count ?? 0)
            
            // 给其设置约束
            self.snp_updateConstraints { (make) in
                
                make.size.equalTo(size)
            }
           // 刷新UI D(当拿到数据 第一时间要刷新)
            
            reloadData()
        }
    
    }
    // 通过多少张配图计算配图size
    func getPictureViewSize(count: Int) -> CGSize {
        
       
        
        // 计算行数 和列数
         let col = count == 4 ? 2 :(count >= 3 ? 3 : count)
        
         let row = count == 4 ? 2 : ((count - 1) / 3 + 1)
        
        // 计算配图宽高        
        let w = CGFloat(col) * cellWH + CGFloat(col - 1) * itmeMargin
        
        let h = CGFloat(row) * cellWH + CGFloat(row - 1) * itmeMargin
        
        
        return CGSize(width: w, height: h)
        
        
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setUpUI()
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 设置视图
 
    func setUpUI(){
     
       backgroundColor = UIColor.clearColor()
        
       //注册cell的Class
        registerClass(WTPictureViewCell.self, forCellWithReuseIdentifier: pictureViewrusedId)
        // 设置数据源
        dataSource = self
        
        let flowlayout = collectionViewLayout as! UICollectionViewFlowLayout
        // 设置item
        flowlayout.itemSize = CGSize(width: cellWH, height: cellWH)
         // 设置垂直间距和水平间距
        flowlayout.minimumLineSpacing = itmeMargin
        flowlayout.minimumInteritemSpacing = itmeMargin
        
        // 添加控件
       addSubview(countLabel)
         // 添加约束
      countLabel.snp_makeConstraints { (make) in
        
        make.center.equalTo(self)
        
        }
    
    
    
    }
    
    
    // MARK: - 懒加载控件
    private lazy var countLabel: UILabel = UILabel(textColor: UIColor.redColor(), textFont: 35)

}


extension WTPictureView: UICollectionViewDataSource{

  
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
         return pic_urls?.count ?? 0
        
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureViewrusedId, forIndexPath: indexPath) as! WTPictureViewCell
        // cell 设置随机颜色
//        cell.backgroundColor = RandomColor()
        // 给cell赋值
        cell.pictureInfo = pic_urls![indexPath.item]
        
        return  cell
    }
    
}
/// 自定义cell
class WTPictureViewCell: UICollectionViewCell {
    // 供外界给其赋值
    var pictureInfo: WTPictureInfo?{
    
        didSet{
        
            
        // 设置图片
            placeholderImageView.sd_setImageWithURL(NSURL(string: pictureInfo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
        
        }
    
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setUpUI(){
   
        backgroundColor = UIColor.whiteColor()
        // 添加控件
        contentView.addSubview(placeholderImageView)
        // 添加约束
        placeholderImageView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }

    }
    
    // MARK: - 懒加载控件 timeline_card_top_background
    
    private lazy var placeholderImageView: UIImageView = {
    
        let imageView = UIImageView(imageName: "timeline_card_top_background")

         // 指定图片填充方式
        
         imageView.contentMode = .ScaleAspectFill
        
         imageView.clipsToBounds = true
        
         return imageView
    
    }()
    
    
    
    
}


